<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRoute } from 'vue-router';
import { getDoctorDetail, type DoctorVO } from '@/apis/doctor/doctorAPI';

// 获取路由参数
const route = useRoute();
const doctorId = ref<number>(Number(route.query.id) || 0);
const loading = ref<boolean>(false);

// 医生详情数据
const doctorInfo = ref<DoctorVO>({
  id: 0,
  name: '',
  title: '',
  department: '',
  hospital: '',
  introduction: '',
  goodAt: '',
  avatar: ''
});

// 根据ID获取医生信息
const fetchDoctorDetail = async () => {
  if (!doctorId.value) return;

  loading.value = true;
  try {
    const res = await getDoctorDetail(doctorId.value);
    if (res.status === 200 && res.data) {
      doctorInfo.value = res.data;
    } else {
      console.error('获取医生详情失败:', res.message);
    }
  } catch (error) {
    console.error('获取医生详情异常:', error);
  } finally {
    loading.value = false;
  }
};

onMounted(() => {
  fetchDoctorDetail();
});

const handleAppointment = () => {
  // 处理预约逻辑
  console.log('预约医生:', doctorId.value);
};
</script>

<template>
  <div class="doctor-detail">
    <el-card v-loading="loading" class="doctor-card">
      <div class="basic-info">
        <div class="avatar">
          <el-avatar :size="120" :src="doctorInfo.avatar" />
        </div>
        <div class="info-content">
          <div class="name-title">
            <span class="name">{{ doctorInfo.name }}</span>
            <span class="title">{{ doctorInfo.title }}</span>
            <el-button type="primary" class="appointment-btn" @click="handleAppointment">预约</el-button>
          </div>
          <div class="hospital">{{ doctorInfo.hospital }}</div>
          <div class="department">{{ doctorInfo.department }}</div>
        </div>
      </div>
      <el-divider />
      <div class="detail-section">
        <h3>简介</h3>
        <p class="introduction">{{ doctorInfo.introduction || '暂无简介' }}</p>
      </div>
      <div class="detail-section">
        <h3>擅长</h3>
        <p class="good-at">{{ doctorInfo.goodAt || '暂无相关信息' }}</p>
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.doctor-detail {
  padding: 20px;
}

.doctor-card {
  background: #fff;
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.basic-info {
  display: flex;
  margin-bottom: 24px;
}

.avatar {
  margin-right: 24px;
}

.info-content {
  flex: 1;
}

.name-title {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
}

.name {
  font-size: 24px;
  font-weight: bold;
  margin-right: 12px;
}

.title {
  font-size: 16px;
  color: #409EFF;
  background-color: rgba(64, 158, 255, 0.1);
  padding: 2px 8px;
  border-radius: 4px;
}

.hospital {
  font-size: 16px;
  color: #606266;
  margin-bottom: 8px;
}

.department {
  font-size: 14px;
  color: #909399;
}

.appointment-btn {
  margin-left: auto;
}

.detail-section {
  margin-top: 20px;
}

.detail-section h3 {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 12px;
  color: #303133;
}

.introduction, .good-at {
  font-size: 14px;
  line-height: 1.6;
  color: #606266;
}
</style>
